<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="4" :sm="8">
            <a-form-item label="经销商编号">
              <a-input placeholder="请输入经销商编号" v-model="queryParam.storeId"></a-input>
            </a-form-item>
          </a-col>
          <a-col  :md="4" :sm="8">
            <a-form-item label="经销商名称">
              <a-input placeholder="请输入经销商名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="销售人员">
              <a-input placeholder="请输入销售人员" v-model="queryParam.saleRealName"></a-input>
            </a-form-item>
          </a-col>
          <a-col  :md="4" :sm="8">
            <a-form-item
              label="渠道">
              <a-select
                v-decorator="[ 'channelId', {}]"
                v-model="queryParam.channelIds"
                style="width: 100%"
                :getPopupContainer="node => node.parentNode"
                placeholder="请选择渠道">
                <a-select-option v-for="(channel,channelIdIndex) in optChannelList" :key="channel.id.toString()"
                                 :value="channel.id">
                  {{ channel.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>

            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length
        }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="storeId"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a @click="addressAdd(record)" style="margin-left: 8px">新增地址</a>
          <a @click="handleShowDetail(record)" style="margin-left: 8px">查看地址</a>
          <a @click="handleAccount(record)" v-if="record.account==null" style="margin-left: 8px">设置账号</a>
          <a @click="handleAccount(record)" v-else style="margin-left: 8px">重置密码</a>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->
    <a-tabs defaultActiveKey="0">
      <a-tab-pane tab="可售物料" :key="refKeys[0]" :forceRender="true">
        <a-tabs defaultActiveKey="1" @change="handleChangeTabs">
          <a-tab-pane v-for="(channel, index) in channelList" :key="channel.channelId" :tab="channel.channelName" :forceRender="true">
          </a-tab-pane>
        </a-tabs>
        <stm-channel-product-list2 ref="StmChannelProductList" ></stm-channel-product-list2>
      </a-tab-pane>
      <a-tab-pane tab="网点关联" :key="refKeys[1]" :forceRender="true">
        <stmDealerSalesRelation-ListJX ref="StmDealerSalesRelationListJX"></stmDealerSalesRelation-ListJX>
      </a-tab-pane>
    </a-tabs>
    <!-- 表单区域 -->
    <stmDealerInfo-modalJX ref="modalForm" @ok="modalFormOk"></stmDealerInfo-modalJX>
    <stm-save-address ref="stmSaveAddress" @ok="modalFormOk"/>
    <stm-member-address-list ref="stmMemberAddressList" @ok="modalFormOk"/>
    <stmSetAccount ref="StmSetAccount" @ok="modalFormOk"/>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import StmDealerInfoModalJX from './modules/StmDealerInfoModalJX'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import StmDealerSalesRelationListJX from './StmDealerSalesRelationListJX'
import {httpAction, getAction} from '@/api/manage'
import StmSaveAddress from "@views/stm/modules/StmSaveAddress"
import StmMemberAddressList from "@views/stm/modules/StmMemberAddressList"
import StmSetAccount from './modules/StmSetAccount'
import StmChannelProductList2 from './StmChannelProductList'

export default {
  name: 'StmDealerInfoListJX',
  mixins: [JeecgListMixin],
  components: {
    StmDealerInfoModalJX,
    StmDealerSalesRelationListJX,
    StmSaveAddress,
    StmMemberAddressList,
    StmSetAccount,
    StmChannelProductList2
  },
  data() {
    return {
      refKeys: ['stmStoreSalesRelation',],
      description: '经销商列表页面',
      channelList: [],
      optChannelList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '经销商编号',
          align: 'center',
          dataIndex: 'storeId'
        },
        {
          title: '经销商名称',
          align: 'center',
          width: 120,
          dataIndex: 'name'
        },
        {
          title: '渠道',
          align: 'center',
          width: 120,
          dataIndex: 'channelName'
        },
        {
          title: '联系人',
          align: 'center',
          dataIndex: 'linkName'
        },
        {
          title: '联系电话',
          align: 'center',
          dataIndex: 'phone'
        },
        {
          title: '联系电话',
          align: 'center',
          dataIndex: 'phone2'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status_dictText'
        },
        {
          title: '销售人员',
          align: 'center',
          dataIndex: 'saleRealName'
        },
        {
          title: '经销商账号',
          align: 'center',
          dataIndex: 'account'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {customRender: 'action'}
        }
      ],
      type: "radio",
      url: {
        list: '/stm/stmStoreInfo/list',
        delete: '/stm/stmStoreInfo/delete',
        generate: '/stm/stmStoreInfo/generateSkuAndStock',
        deleteBatch: '/stm/stmStoreInfo/deleteBatch',
        exportXlsUrl: 'stm/stmStoreInfo/exportXls',
        importExcelUrl: 'stm/stmStoreInfo/importExcel',
        storeChannelList:  "/stm/stmChannelStoreRelation/list",
        channelListUrl: 'stm/stmChannelInfo/list',
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getChannelList();
    this.queryParam.storeType = '1';
    this.queryParam.brandCategory = 1;
  },
  methods: {
    clickThenCheck(record) {
      console.log("aaaaa");
      return {
        on: {
          click: () => {
            this.onSelectChange(record.storeId.toString().split(","), [record]);
          }
        }
      };
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      console.log(7777777)
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      this.$refs.StmDealerSalesRelationListJX.getMain(this.selectedRowKeys[0]);
      this.getChannelProduct(this.selectedRowKeys[0]);
    },
    getChannelProduct(id){
      let param={
        storeId: id,
        pageNo: 1,
        pageSize: 10
      }
      getAction(this.url.storeChannelList, param).then((res) => {
        if (res.success) {
         this.channelList=res.result.records;
         if(this.channelList.length>0){
           this.$refs.StmChannelProductList.getMain(this.channelList[0].channelId);
         }
         else{
           this.$refs.StmChannelProductList.getMain(-1);
         }
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
      this.$refs.StmDealerSalesRelationListJX.queryParam.mainId = null;
      this.$refs.StmDealerSalesRelationListJX.loadData();
      this.$refs.StmDealerSalesRelationListJX.selectedRowKeys = [];
      this.$refs.StmDealerSalesRelationListJX.selectionRows = [];
    },

    handleDelete: function (id) {
      let that = this;
      deleteAction(that.url.delete, {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.loadData();
          this.$refs.StmDealerSalesRelationListJX.loadData();
        } else {
          that.$message.warning(res.message);
        }
      });
    },
    addressAdd(record) {
      this.$refs.stmSaveAddress.title = "新增地址";
      this.$refs.stmSaveAddress.show(record);
    },
    handleShowDetail(record) {
      this.$refs.stmMemberAddressList.title = "查看地址";
      this.$refs.stmMemberAddressList.init(record);
    },
    searchQuery: function () {
      this.selectedRowKeys = [];
      this.selectionRows = [];
      this.$refs.StmDealerSalesRelationListJX.queryParam.mainId = null;
      this.$refs.StmDealerSalesRelationListJX.loadData();
      this.$refs.StmDealerSalesRelationListJX.selectedRowKeys = [];
      this.$refs.StmDealerSalesRelationListJX.selectionRows = [];
      this.loadData(1);
    },
    handleAccount(record) {
      this.$refs.StmSetAccount.title = "设置账号";
      this.$refs.StmSetAccount.show(record);
    },
    handleChangeTabs(key) {
      this.$refs.StmChannelProductList.getMain(key);
    },
    getChannelList() {
      let param={};
      param.status=1;
      param.pageSize=100;
      getAction(this.url.channelListUrl, param).then(res => {
        if (res.success) {
          this.optChannelList = res.result.records;
          this.optChannelList.push({id:"",name:"请选择渠道"})
          this.optChannelList.reverse();
        } else {
          console.log(res.message)
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
